import instance from '@/utils/axios'

// 获取频道列表
const getChannels = () => {
  return instance({
    url: '/v1_0/user/channels',
    method: 'get'
  })
}

// 获取文章列表
const getArticles = ({ channel_id, timestamp }) => {
  return instance({
    url: '/v1_0/articles',
    method: 'get',
    params: {
      with_top: 1, // 是否置顶
      channel_id, // 频道ID
      timestamp // 时间戳，请求新的推荐数据传当前的时间戳，请求历史推荐传指定的时间戳
    }
  })
}

// 获取文章详情
const getArticleInfo = ({ id }) => {
  return instance({
    url: '/v1_0/articles/' + id,
    method: 'get'
  })
}
// 关注用户
const followUser = ({ target }) => {
  return instance({
    url: '/v1_0/user/followings',
    method: 'post',
    data: { target }
  })
}

// 取消用户
const unFollowUser = ({ target }) => {
  return instance({
    // 这个接口的id直接在路径后面拼接  格式： url/target
    url: '/v1_0/user/followings/' + target,
    method: 'delete'
  })
}

// 点赞
const addPraise = id => {
  return instance({
    method: 'post',
    url: '/v1_0/article/likings',
    data: {
      target: id
    }
  })
}

// 取消点赞
const cancelPraise = id => {
  return instance({
    method: 'delete',
    // 这个接口的id直接在路径后面拼接  格式： url/target
    url: '/v1_0/article/likings/' + id
  })
}

// 添加不喜欢
const addDisLike = id => {
  return instance({
    url: '/v1_0/article/dislikes',
    method: 'post',
    // post 请求有一层 data
    data: {
      target: id
    }
  })
}

// 取消不喜欢
const cancelDisLike = id => {
  return instance({
    url: '/v1_0/article/dislikes/' + id,
    method: 'delete'
  })
}

// 推荐列表
const getSuggestionList = q => {
  return instance({
    url: '/v1_0/suggestion',
    method: 'get',
    params: {
      q
    }
  })
}

// 搜索结果
const getSearchList = ({ page, per_page, q }) => {
  return instance({
    url: '/v1_0/search',
    method: 'get',
    params: {
      page,
      per_page,
      q
    }
  })
}

//导出接口

export {
  getChannels,
  getArticles,
  getArticleInfo,
  followUser,
  unFollowUser,
  addPraise,
  cancelPraise,
  addDisLike,
  cancelDisLike,
  getSuggestionList,
  getSearchList
}
